package com.xpgk.mvc.common;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.ZtreeNode;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.dept.Department;
import com.xpgk.mvc.dept.DepartmentService;
import com.xpgk.mvc.intoPersonnel.IntoPersonnel;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.organization.OrganizationService;
import com.xpgk.mvc.user.User;
import com.xpgk.tools.ToolSqlXml;

public class CommonController extends BaseController {
	
	/**
	 * 顾客列表select
	 */
	public static final String sqlId_splitPageSelect = "sys.common.splitPageSelect";
	
	/**
	 * 顾客列表from
	 */
	public static final String sqlId_splitPageFrom = "sys.common.splitPageFrom";
	/**
	 * 查询通用功能-市场负责人下拉框
	 */
	public static final String sqlId_getOrganManger = "sys.common.getOrganManger";
	/**
	 * 关于
	 */
	public void about(){	
		render("about.html");
	}
	/**
	 * 关于
	 */
	public void doc(){	
		render("doc.html");
	}
	/**
	 * 获取指定机构层级信息
	 */
	public void getOrganLevelInfo(){
		String sql = "select * from sys_organ_level_info where levelIds = ?";
		Record r = Db.findFirst(sql, ids);
		renderJson(r);
	}
	
	/**
	 * 行政区划
	 */
	public void xzqh() {
		String xzqh = getPara("xzqh_code");
		List<Code> codes = new ArrayList<>();
		if(null != xzqh){
			if(xzqh.equals("0")){
				codes = Code.cacheGetChild(Code.xzqh_ids);
			}else{
				codes = Code.cacheGetChildByCode(xzqh);
			}
		}
		renderJson(codes);
		
	}
	
	/**
	 * 行政区划树形图
	 */
	public void xzqhTree() {
	/*	if (StringUtils.isNotBlank(getPara("xzqh_code"))) {
			ids = Code.cacheGet(getPara("xzqh_code")).getStr(Code.column_ids);
		}*/
		if (null == ids) {
			ids = Code.xzqh_ids;
		}
		List<Code> list = Code.cacheGetChild(ids);
		
		List<ZtreeNode> nodeList = new ArrayList<ZtreeNode>();
		ZtreeNode node = null;

		for (Code code : list) {
			node = new ZtreeNode();
			node.setId(code.getStr(Code.column_ids));
			node.setName(code.getStr(Code.column_name));
			//node.setIsParent(code.getStr(Code.column_isparent));
			node.setIsParent(false+"");
			node.setIcon(getCxt() + ConstantRender.icon_path + code.getStr(Code.column_icon));
			nodeList.add(node);
		}
		renderJson(nodeList);
	}
	
	

	/**
	 * 直辖市行政区划树形图
	 */
	public void xzqhTree2() {
	/*	if (StringUtils.isNotBlank(getPara("xzqh_code"))) {
			ids = Code.cacheGet(getPara("xzqh_code")).getStr(Code.column_ids);
		}*/
		if (null == ids) {
			ids = Code.xzqh_ids;
		}
		//List<Code> list = Code.cacheGetChild(ids);
		
		Code c = Code.dao.findById(ids);
		String parentIds = c.getStr(Code.column_parentids);
		
		String sql = "select * from sys_code where parentids = ? and `status` = '1'";
		List<Code> list = Code.dao.find(sql,ids);
		if(parentIds!=null){
			if(parentIds.equals("e7a14eecef114bbc9dfce3be4e7df297")||parentIds.equals("e9a19da9c7d84bd6af8a4a118098a551")||parentIds.equals("90fdeaa85adb4428a76ad638116b97b9")||parentIds.equals("6ebd96f156494a61989edccb3910a1df")){
				sql = "select * from sys_code where ids = ? and `status` = '1'";
				list = Code.dao.find(sql,ids);
			}
		}
		List<ZtreeNode> nodeList = new ArrayList<ZtreeNode>();
		ZtreeNode node = null;

		for (Code code : list) {
			node = new ZtreeNode();
			node.setId(code.getStr(Code.column_ids));
			node.setName(code.getStr(Code.column_name));
			//node.setIsParent(code.getStr(Code.column_isparent));
			node.setIsParent(false+"");
			node.setIcon(getCxt() + ConstantRender.icon_path + code.getStr(Code.column_icon));
			nodeList.add(node);
		}
		renderJson(nodeList);
	}
	
	
	/**
	 * 查询通用功能-市场负责人下拉框
	 */
	public void getOrganManger(){
		String sql = ToolSqlXml.getSqlMy(sqlId_getOrganManger);
		List<Record> mangers = Db.find(sql, ids);
		renderJson(mangers);
	}
	
	/**
	 * 查询通用功能-产品类型下拉框列表
	 */
	public void getProductTypes(){
		List<Code> codes = Code.cacheGetChild(Code.cplx_ids);
		renderJson(codes);
	}
	
	/**
	 * 查询通用功能-获取指定类型的产品下拉列表
	 */
	public void getProducts(){
	}
	
	/**
	 * 查询通用功能 - 获取当前用户下的机构树形图
	 */
	public void getOrgenTree(){
		List<ZtreeNode> nodeList = new OrganizationService().childNodeData(getCxt(), ids, getCUserOrganIds());
		renderJson(nodeList);
	}
	
	
	/**
	 * 查询通用功能 - 获取总部下的机构树形图
	 */
	public void getOrgenTree2(){
		List<ZtreeNode> nodeList = new OrganizationService().childNodeData2(getCxt(), ids);
		renderJson(nodeList);
	}
	
	/**
	 * 查询通用功能 - 获取指定的机构树形图
	 */
	public void getBeOrgenTree(){
		String organIds = getPara("organIds", getCUserOrganIds());
		List<ZtreeNode> nodeList = new OrganizationService().childNodeData(getCxt(), ids, organIds);
		renderJson(nodeList);
	}
	
	/**
	 * 查询通用功能 - 获取指定机构下的部门树形图
	 */
	public void getDempTree(){
		String organIds = getPara("organIds", getCUserOrganIds());
		String sql = getSqlMy(Department.sqlId_findRootByOrgan);
		Department rootDept = Department.dao.findFirst(sql, organIds);
		List<ZtreeNode> nodeList = new DepartmentService().childNodeData(getCxt(), ids, rootDept.getPKValue());
		renderJson(nodeList);
	}
	
	
	/**
	 *  查询通用功能 - 获取指定机构下所有人员
	 */
	public void getUsersByOrgan(){
		
		List<User> users = Organization.dao.getUsers(ids);
		renderJson(users);
		
	}
	
	/**
	 *  查询通用功能 - 获取指部门下所有人员
	 */
	public void getUsersByDept(){
		String userSql = getSqlMy(User.sqlId_findUserByDept);
		List<User> users = User.dao.find(userSql, ids);
		renderJson(users);
	}

}
